import 'dart:convert';

import 'package:design_mode_flutter/adapter_mode/object_adapter/data.dart';

class Veggie {
  late String name;

  Veggie({required this.name});
}

abstract class IVeggiesAdapter {
  List<Veggie> getVeggies();
}

///适配json数据
class JsonVeggiesAdapter implements IVeggiesAdapter {
  final JsonVeggiesApi _api = JsonVeggiesApi();

  @override
  List<Veggie> getVeggies() {
    final veggiesJson = _api.getVeggiesJson();
    final veggiesList = _parseContactsJson(veggiesJson);
    return veggiesList;
  }

  List<Veggie> _parseContactsJson(String contactsJson) {
    final contactsMap = json.decode(contactsJson) as Map<String, dynamic>;
    final contactsJsonList = contactsMap['veggies'] as List;
    final contactsList = contactsJsonList.map((json) {
      final contactJson = json as Map<String, dynamic>;
      return Veggie(
        name: contactJson['name'] as String,
      );
    }).toList();
    return contactsList;
  }
}